/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.thinkgem.jeesite.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 错误数据管理Controller
 * @author xiaoqian
 * @version 2014-03-26
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/sys/errorData")
public class ErrorDataController extends BaseController {

	@Autowired
	private  WechatMemberService wechatMemberService;
	
	@Autowired
	private  WechatDaoHelper wechatDaoHelper;
	
	@ModelAttribute
	public WechatMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return wechatMemberService.get(id);
		}else{
			return new WechatMember();
		}
	}
	
	@RequiresPermissions("sys:errorData:view")
	@RequestMapping(value = {"list", ""})
	public String list(WechatMember wechatMember, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			wechatMember.setUser(user);
		}
		List<Map<String,Object>> list = wechatDaoHelper.getErrorData(); 
        
        model.addAttribute("list", list);
		return "modules/sys/errorDataList";
	}

	@RequiresPermissions("sys:errorData:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		String mobile=request.getParameter("mobile");
		String user_id=request.getParameter("user_id");
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("mobile", mobile);
		params.put("user_id", user_id);
		
		 List<Map<String, Object>>  list=wechatDaoHelper.getMemberByMobile(params);
		 
		 model.addAttribute("list", list);
		 model.addAttribute("mobile", mobile);
		 model.addAttribute("user_id", user_id);
		
		return "modules/sys/errorDataForm";
	}

	
	@RequiresPermissions("sys:errorData:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response,Model model) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("id", id);
		String mobile=request.getParameter("mobile");
		String user_id=request.getParameter("user_id");
		
		wechatDaoHelper.deleteMemberById(params);
		addMessage(redirectAttributes, "删除错误数据管理成功");
		return "redirect:"+Global.ADMIN_PATH+"/sys/errorData/form?mobile="+mobile+"&user_id="+user_id;
	}

}
